<?php
	require_once("discuss_func.inc.php");
	if(isset($_REQUEST['pid']))
		$pid=intval($_REQUEST['pid']); 
	else
		$pid=0;
	if(isset($_REQUEST['cid']))
		$cid=intval($_REQUEST['cid']);
	else
		$cid=0;
	$prob_exist = problem_exist($pid, $cid);
	if ($cid!='' && $cid!=null && $prob_exist) 
		require_once("contest-header.php");
	else 
		require_once("oj-header.php");
	echo "<title>论坛</title>";
?>


<?php
if ($prob_exist){?>



<ul class="breadcrumb">
<li>
	    <?php if ($cid!=null) echo "<a href=\"discuss.php?cid=".$cid."\">Contest ".$cid."</a>"; else echo "<a href=\"discuss.php\">论坛</a>";
if ($pid!=null && $pid!=0) echo " <span class='divider'>/</span> <a href=\"discuss.php?pid=".$pid."&cid=".$cid."\">P ".$pid."</a>";?>
   
</li>
</ul>



<?php }
$sql = "SELECT `tid`, `title`, `top_level`, `topic`.`status`, `cid`, `pid`, CONVERT(MIN(`reply`.`time`),DATE) `posttime`, MAX(`reply`.`time`) `lastupdate`, `topic`.`author_id`, COUNT(`rid`) `count` FROM `topic`, `reply` WHERE `topic`.`status`!=2 AND `reply`.`status`!=2 AND `tid` = `topic_id`";
if (array_key_exists("cid",$_REQUEST)&&$_REQUEST['cid']!='') $sql.= " AND ( `cid` = '".mysql_escape_string($_REQUEST['cid'])."'";
else $sql.=" AND ( ISNULL(`cid`)";
$sql.=" OR `top_level` = 3 )";
if (array_key_exists("pid",$_REQUEST)&&$_REQUEST['pid']!=''){
  $sql.=" AND ( `pid` = '".mysql_escape_string($_REQUEST['pid'])."' OR `top_level` >= 2 )";
  $level="";
}
else
  $level=" - ( `top_level` = 1 AND `pid` != 0 )";
$sql.=" GROUP BY `topic_id` ORDER BY `top_level`$level DESC, MAX(`reply`.`time`) DESC";
$sql.=" LIMIT 30";
//echo $sql;
$result = mysql_query($sql) or die("Error! ".mysql_error());
$rows_cnt = mysql_num_rows($result);
$cnt=0;
$isadmin = isset($_SESSION['administrator']);
?>

<table class="table table-condensed">
    <thead>
    <tr>
	    <th width=5%><a href="newpost.php<?php if ($pid!=0 && $cid!=null) echo "?pid=".$pid."&cid=".$cid;
else if ($pid!=0) echo "?pid=".$pid;
else if ($cid!=null) echo "?cid=".$cid;?>" class="btn btn-info btn-xs">发帖</a></th>
        <th width=5%>题号</th>
        <th width=10%>作者</th>
		<th>标题</th>
		<th width=10%>发表日期</th>
		<th width=5%>回复</th>
    </tr>
    </thead>
    <tbody>
	

<?php if ($rows_cnt==0) echo("<tr class=\"evenrow\"><td colspan=4></td><td style=\"text-align:center\">毫无新主题啊.</td></tr>");

for ($i=0;$i<$rows_cnt;$i++){
	mysql_data_seek($result,$i);
	$row=mysql_fetch_object($result);
	echo "<tr>";
	echo "<td>";
		if ($row->top_level!=0){
			if ($row->top_level!=1||$row->pid==($pid==''?0:$pid))
			//echo"<b class=\"Top{$row->top_level}\">Top</b>";
			echo "<span class='label label-danger'>置顶</span>";
		}
		else if ($row->status==1) echo"<span class='label label-danger'>LOCK</span>";
		else if ($row->count>20) echo"<span class='label label-danger'>Hot!</span>";
	echo "</td>";
	echo "<td>";
	if ($row->pid!=0) echo"<a href=\"discuss.php?pid={$row->pid}&cid={$row->cid}\">{$row->pid}</a>";else echo "<a>----</a>";
	echo "</td>";
	echo "<td><a href=\"../userinfo.php?user={$row->author_id}\">{$row->author_id}</a></td>";
	echo "<td><a href=\"thread.php?tid={$row->tid}&cid={$row->cid}\">".nl2br(htmlspecialchars($row->title))."</a></td>";
	echo "<td>{$row->posttime}</td>";
	echo "<td>".($row->count-1)."</td>";
	echo "</tr>";
}
mysql_free_result($result);

?>
            </tbody>
</table>


<?php require_once("../oj-footer.php")?>
